#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import time
import datetime
import winsound
from threading import Thread


class TimingClock(Thread):
    c = 'r'
    count_down = None
    count = 0
    pause_timeout = 0
    pause_timestamp = ""

    def __init__(self):
        super(TimingClock, self).__init__()

    def setCountDown(self, count_down):
        self.count_down = count_down

    def done(self):
        sound = "欢快节奏韵律音效.wav"
        winsound.PlaySound(sound, winsound.SND_FILENAME|winsound.SND_NOWAIT )

    def setState(self, c):
        self.c = c
        if c == "s":
            self.pause_timeout = 0
            self.pause_timestamp = ""
        elif c == "i":
            t = datetime.datetime.now()
            self.pause_timestamp = t.strftime("%H:%M")
            print("Pause Time:", self.pause_timestamp)

    def run(self):
        while True:
            if self.c == 'i':
                time.sleep(1)
                self.pause_timeout = self.pause_timeout + 1
                if self.pause_timeout > 1800: # 30*60
                    print("[Auto Record] Counted:", self.count)
                    break
                continue
            if self.c == 'c' or self.c == 'e':
                break

            # 倒计时
            if self.count_down:
                left = self.count_down - self.count
                if left < 0:
                    over_left = 0 - left
                    hours,rem = divmod(over_left, 3600)
                    minutes,seconds = divmod(rem, 60)
                    print(" [Left]  + %02d:%02d:%02d" % (hours,minutes,seconds), end='\r')
                else:
                    hours,rem = divmod(left, 3600)
                    minutes,seconds = divmod(rem, 60)
                    print(" [Left]  %02d:%02d:%02d" % (hours,minutes,seconds), end='\r')

                if left == 0:
                    self.done()
                    break

            # 正计时
            else:
                hours,rem = divmod(self.count, 3600)
                minutes,seconds = divmod(rem, 60)
                print(" [Count]  %02d:%02d:%02d" % (hours,minutes,seconds), end='\r')

            time.sleep(1)
            self.count = self.count + 1


if __name__ == '__main__':
    m = int( input("请输入定时分钟数:") )
    t = TimingClock()
    if m:
        t.setCountDown(m*60)
    t.start()

    while True:
        c = input()
        if c == 'i' or c == 's':
            if t.is_alive():
                t.c = c
        if c == 'c' or c == 'e':
            if t.is_alive():
                t.c = c
            break

    t.join()